package GUI.TabPanels.AdminTabs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import comparators.IntegerComparator;

import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class CreateHotelPanel extends JPanel{
	private JButton add_hotel_button;

	private JLabel hotel_name_label;
	private JTextField hotel_name_text;

	private JLabel hotel_address_label;
	private JTextField hotel_address_text;

	private JLabel hotel_city_label;
	private JTextField hotel_city_text;

	private JLabel hotel_country_label;
	private JTextField hotel_country_text;

	private JLabel hotel_phno_label;
	private JTextField hotel_phno_text;

	private JTable roomTable;

	private int TFWidth = 15;

	private JLabel emptyLabel;

	private JPanel topPanel;
	private JPanel middlePanel;

	private JScrollPane scrollPane;

	private String[] columnNames =
		{
			"ID",
			"Name",
			"Address",
			"City",
			"Country",
			"Phone Number"
		};

	public CreateHotelPanel(){
		this.setBackground(Color.PINK);
		this.setLayout(new BorderLayout());
		this.SetUpTopPanel();
		this.SetUpMiddlePanel();
		this.SetUpBottomPanel();
	}

	public void SetUpTopPanel(){
		topPanel = new JPanel();
		topPanel.setBackground(Color.pink);
		topPanel.setLayout(new GridLayout(0, 2, -300, 7));

		hotel_name_label = new JLabel("Hotel Name:");
		hotel_name_text = new JTextField(TFWidth);
		hotel_name_text.setEditable(true);
		topPanel.add(hotel_name_label);
		topPanel.add(hotel_name_text);

		hotel_address_label = new JLabel("Address:");
		hotel_address_text = new JTextField(TFWidth);
		hotel_address_text.setEditable(true);
		topPanel.add(hotel_address_label);
		topPanel.add(hotel_address_text);

		hotel_city_label = new JLabel("City:");
		hotel_city_text = new JTextField(TFWidth);
		hotel_city_text.setEditable(true);
		topPanel.add(hotel_city_label);
		topPanel.add(hotel_city_text);

		hotel_country_label = new JLabel("Country:");
		hotel_country_text = new JTextField(TFWidth);
		hotel_country_text.setEditable(true);
		topPanel.add(hotel_country_label);
		topPanel.add(hotel_country_text);

		hotel_phno_label = new JLabel("Phone Number:");
		hotel_phno_text = new JTextField(TFWidth);
		hotel_phno_text.setEditable(true);
		topPanel.add(hotel_phno_label);
		topPanel.add(hotel_phno_text);

		add_hotel_button = new JButton("Add Hotel");
		add_hotel_button.addActionListener(new AddHotelButton());

		this.add(topPanel, BorderLayout.NORTH);
	}

	public void SetUpMiddlePanel(){
		emptyLabel = new JLabel();

		middlePanel = new JPanel();
		middlePanel.setBackground(Color.pink);

		middlePanel.add(emptyLabel);
		middlePanel.add(add_hotel_button);
		middlePanel.add(emptyLabel);

		this.add(middlePanel, BorderLayout.CENTER);
	}

	public void SetUpBottomPanel(){
		Object[][] tableData = Controller.GetHotelsWithDetails();

		CreateTable(tableData);
	}

	private void Update()
	{
		Object[][] tableData = Controller.GetHotelsWithDetails();

		CreateTable(tableData);

		updateUI();

		this.setVisible(false);
		this.setVisible(true);
	}

	private void CreateTable(Object[][] tableData) {
		roomTable = new JTable(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false; 
			} 
		};
		roomTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableModel model = new DefaultTableModel(tableData, columnNames);

		TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

		sorter.setComparator(0, new IntegerComparator());

		roomTable.setRowSorter(sorter);
		roomTable.setAutoCreateRowSorter(false);

		roomTable.setPreferredScrollableViewportSize(new Dimension(500, 300));
		roomTable.setFillsViewportHeight(true);

		if(scrollPane!=null)
			this.remove(scrollPane);
		scrollPane = new JScrollPane(roomTable);
		this.add(scrollPane, BorderLayout.SOUTH);
	}

	class AddHotelButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try
			{
				Long.parseLong(hotel_phno_text.getText());
				if (hotel_name_text.getText().compareTo("") == 0 
						|| hotel_address_text.getText().compareTo("") == 0 
						|| hotel_city_text.getText().compareTo("") == 0
						|| hotel_country_text.getText().compareTo("") == 0
						|| hotel_phno_text.getText().compareTo("") == 0
						|| hotel_name_text.getText().length() < 3)
					throw new Exception();
				String[] data = { hotel_name_text.getText(),
						hotel_address_text.getText(),
						hotel_city_text.getText(),
						hotel_country_text.getText(),
						hotel_phno_text.getText() };

				JOptionPane.showMessageDialog(null, "Hotel Created!");
				Controller.CreateHotel(data);

				Update();
			}
			catch (NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Phone Number must be an integer.");
			}
			catch (Exception e)
			{
				JOptionPane.showMessageDialog(null, "Cannot accept empty fields. Name must be at least 3 characters.");
			}
		}
	}
}
